<?php
include('header.php');
	//Max posts per page
	$limit = 10;
	
	//What page are we at
	if(isset($_GET['page'])) {
		$page = $_GET['page'];
	} else {
		$page = 1;
	}
	
	//If the search function has been submitted
	if(isset($_POST['search'])) {
		// Use the MongoRegEx class to specify the search query
		$regex = new MongoRegEx('/'.$_POST['search'].'/i');
		$query = array('post' => $regex);
	} else {
		//If not, we set the variable $query to an empty array to find all
		$query = array();
	}
	
	//How many post documents we are going to retrieve
	$offset = ($page-1) * $limit;
	
	$nextpage = ($page +1);
	$prevpage = ($page -1);

	//This block ($query1_start, $query1_end, $query1_time) will be repeated for each queries
	$query1_start = timeit();
	//Execute a search and store the posts under the cursor variable and sort the result on _id, descending order
	$cursor = q('post')->find($query)->skip($offset)->limit($limit)->sort(array('_id' => -1));
	$query1_end = timeit();
	$query1_time = number_format($query1_end - $query1_start, 15);
		
	//Count how many post document we find
	$postCount = $cursor->count();
		
?>
<div id="container">
	<div class="pageheader">
		<h1>BLOG POSTS</h1>
		<h2><?php echo $postCount . " blog posts"; ?></h2>
		<div class="desc">
		</div>
		<div class="cleardiv">
		</div>
	</div>
	<div id="main">
	<?php
		// This block only react when a query is send with success criterier for deletion and add new post
		if(isset($_GET['status']) && ($_GET['status'] == 'success')) {
			$postid = new MongoId($_GET['postid']);
			$query = array('_id' => $postid);
			$fields = array('post_title' => 1);
			
			$document = q('post')->findOne($query, $fields);

			if(isset($_GET['msg']) && ($_GET['msg'] == 'deleted')) {
				echo '<h1 style="color: red;">The post <i>'.$document['post_title'].'</i> has been deleted</h1>';
			} elseif(isset($_GET['msg']) && ($_GET['msg'] == 'newpost')) {
				echo '<h1 style="color: red;">The post <i>'.$document['post_title'].'</i> has been added to the database</h1>';
			}	
		}
	
		//We iterate over the cursor
		while($document = $cursor->getNext()) {
			
			//we call getDBRef, which creates a link between our two collections and is relational by the document _id in the authors collection
			$ref = q()->getDBRef($document['author']);
			$author = $ref['name'];

			$date = date('d. M - Y', $document['date']);
			$time = date('H:i:s', $document['date']);
			
			//We call the class MongoID to get the id
			$postid = new MongoId($document['_id']);
			?>
			<div class="postwarp">
				<div class="postmetas">
					<div class="postmeta1"><span>by :</span>&nbsp; <?php echo $author; ?></div>		
					<div class="postmeta2"><span>On :</span>&nbsp; <?php echo $date; ?></div>
					<div class="postmeta3"><span>at :</span>&nbsp; <?php echo $time; ?></div>
					<div class="postmeta5"><span>tags :</span>&nbsp; <?php echo implode(", ", $document['tags']); ?></div>
				</div>
				<div class="post">
					<div class="post_title" id="36">
						<a href="view.php?postid=<?php echo $postid; ?>" title="<?php echo $document['post_title']; ?>"><?php echo $document['post_title']; ?></a>
					</div>	
					<div class="thumbnail">
						<div class="postthumbp">
							<a href="view.php?postid=<?php echo $postid; ?>" title="<?php echo $document['post_title']; ?>"><img src="images/blogimages/blogimg_01.jpg" alt="<?php echo $document['post_title']; ?>" width="440" height="270" border="0px"/></a>
						</div>
					</div>
					<div class="postcont">
						<p><?php echo $document['post']; ?></p>
					</div>						
				</div>
				<div class="comments">
				<?php
				//We check if there is one or more comments attached to the post
				if(isset($document['comments'])) {
					//We count how many comments there are
					$count = count($document['comments']);
				} else {
					$count = 0;
				}
				
				echo '<a href="view.php?postid='.$postid.'">View Comments ('.$count.')</a>';
				?>
				</div>
				<div class="bmore">
					<a href="view.php?postid=<?php echo $postid; ?>" title="<?php echo $document['post_title']; ?>"><img src="images/more.png"/></a>
				</div>
			</div>
			<?php
			
		}
	?>

	<div class="navigationt">
		<div class='wp-pagenavi'>
			<?php
				$posts = q('post')->find()->count();

				// Create the Next or Privious (or both) links.
				if($page > 1) {
					echo '<span class="pages"><a href="?page='.$prevpage.'">« Previous Page</a></span>';
					if($page * $limit < $posts) {
						echo '<span class="pages"><a href="?page='.$nextpage.'">Next Page »</a></span>';
					}
				} else {
					if($page * $limit < $posts) {
						echo '<span class="pages"><a href="?page='.$nextpage.'">Next Page »</a></span>';
					}
				}
			?>
		</div>
	</div>
	<div class="cleardiv"></div>
</div>
	<div class="sidebar">
		<div id="search_main" class="block">
			<form method="post" name="search" action="posts.php" enctype="multipart/form-data">
				<div>
					<input type="text" class="field" name="search" id="search" value="Search post content ..." onfocus="if (this.value == 'Search post content ...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search post content ...';}"/>
					<input type="image" src="images/sbtn.png" class="submit" name="submit"/>
				</div>
			</form>
		</div>
		
		<div class="widget">
			<div class="widgettitle">
				Navigation
			</div>
			<div class="widgetconten">
				<p><a href="posts.php">View all posts</a></p>
			</div>
		</div>		
		
		<div class="widget">
			<div class="widgettitle">
				Admin section
			</div>
			<div class="widgetconten">
				<p><a href="add.php">Add new post</a></p>
			</div>
		</div>

		<div class="widget">
			<div class="widgettitle">
				MongoDB Query Information
			</div>
			<div class="widgetconten">
				<h3>Information</h3>
				<h3>Query time</h3>
				<p>
				<?php
					// Print the querytime
					if(isset($_GET['query']) && ($_GET['query'] != '')) {
						if($_GET['query'] == 'delete_post') {
							echo 'It took <span>' . $_GET['querytime'] . '</span> seconds to delete the post';
						} elseif($_GET['query'] == 'added_post') {
							echo 'It took <span>' . $_GET['querytime'] . '</span> seconds to add the post';
						}
					}elseif(isset($query1_time) && ($query1_time != '')) {
						echo "It took <span>" . $query1_time . "</span> seconds to show <span>" . $postCount . "</span> posts";
						
					}
				?>
				</p>

			</div>
		</div>

	</div>
</div>
<?php
	include('footer.php');
?>